// pages/act/actAuditDetail/actAuditDetail.js
let {
  api_GetApplyFormDetail,
  api_DealPassAudit,
  api_DealRejectAudit,
  api_SendTemplateMessage
} = require("../../api/getData.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    dataSource: {
      actInfo: {},
      selectedFlag: false, // 参与人展开折叠标记
    },
    rejectFlag: false,
    rejectReason: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('options query===================', options.actApplyFormId)
    /*获取未审核活动详情*/
    let params = options.actApplyFormId
    api_GetApplyFormDetail(params, (res) => {
      console.log("#########--未审核的活动详情--##########", res.data.data)
      let str = 'dataSource.actInfo'
      this.setData({
        [str]: res.data.data
      })
    })


  },

  onPass: function(e) {
    console.log('formId==>', e.detail.formId)
    var that = this
    that.setData({
      rejectFlag: false,
      formId: e.detail.formId
    })
    console.log('要通过的活动ID', that.data.dataSource.actInfo.actApplyFormId)
    let tmp = JSON.stringify(that.data.dataSource.actInfo.actApplyFormId);
    let result = 'actApplyFormId='
    result += tmp
    api_DealPassAudit(result, (res) => {
      console.log('审核结果==>', res)
      if (res.errmsg == 'ok') {
        wx.showToast({
          title: '通过成功！',
          icon: 'success',
          duration: 2000
        })
        //发送模板消息
        that.sendPassTemplateMessage()
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },

  onReject: function(e) {
    this.setData({
      rejectFlag: true
    })
  },

  //拒绝原因
  setRejectReason: function(e) {
    this.setData({
      rejectReason: e.detail.value
    })
  },

  onRejectConfirm: function(e) {
    let that = this
    console.log('formId==>', e.detail.formId)
    console.log('拒绝原因==>', that.data.rejectReason)
    that.setData({
      formId: e.detail.formId
    })
    let tmp = that.data.rejectReason
    if (tmp !== '') {
      console.log('要拒绝的活动ID==>', that.data.dataSource.actInfo.actApplyFormId)
      let result = {
        actApplyFormId: that.data.dataSource.actInfo.actApplyFormId,
        reason: tmp
      }
      console.log('要提交的拒绝信息==>', result)
      api_DealRejectAudit(result, (res) => {
        console.log('审核结果==>', res)
        if (res.errmsg == 'ok') {
          //发送模板消息
          that.sendPassTemplateMessage()
          wx.showToast({
            title: '拒绝成功！',
            icon: 'success',
            duration: 2000
          })
          wx.navigateBack({
            delta: 1
          })
        }
      })
    } else {
      wx.showToast({
        title: '请填写拒绝原因',
        icon: 'none',
      })
    }

  },

  sendPassTemplateMessage: function(e) {
    // 模板消息关键词参数
    let keywords = {
      "keyword1": {
        "value": "同研开发者测试",
        "color": "#173177"
      },
      "keyword2": {
        "value": "英语微沙龙",
        "color": "#173177"
      },
      "keyword3": {
        "value": "英语",
        "color": "#173177"
      },
      "keyword4": {
        "value": "新主楼",
        "color": "#173177"
      },
      "keyword5": {
        "value": "2018-10-04 19:00-21:00",
        "color": "#173177"
      },
    }
    let data = JSON.stringify(keywords);
    let messageData = {
      templateId: 'TkJVwatplFTVp0ZvsHRcfeugqpm1nq_f_qBekHA6Bzo',
      page: '/pages/act/home/home',
      formId: this.data.formId,
      data: data,
      // color: "#173177",
    }
    console.log('==>待发送的模板消息数据：', messageData)
    //调用发送模板消息API
    api_SendTemplateMessage(messageData, (res) => {
      console.log('发送模板消息==>', res)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})